class MusicPlayer {
  var playlist: List[String] = Nil     // 音乐列表
  var currentSong: String = ""         // 当前播放的音乐
  var playMode: String = "loop"        // 播放模式，默认为循环播放

  def addSong(song: String): Unit = {
    playlist = playlist :+ song       // 添加音乐到播放列表
  }

  def removeSong(song: String): Unit = {
    playlist = playlist.filter(_ != song)  // 从播放列表中删除指定音乐
  }

  def play(): Unit = {
    currentSong = playlist.headOption.getOrElse("") // 播放列表中的第一首音乐
  }

  def next(): Unit = {
    // 根据当前播放模式选择下一首音乐
    playMode match {
      case "loop" => loopPlayNext()
      case "shuffle" => shufflePlayNext()
      case "single" => singlePlayNext()
      case _ => println("Unknown play mode!")
    }
  }

  def loopPlayNext(): Unit = {
    val index = playlist.indexOf(currentSong) // 当前音乐在播放列表中的索引
    if (index == playlist.length - 1) {       // 如果当前音乐是最后一首，则回到列表开头
      currentSong = playlist.headOption.getOrElse("")
    } else {                                   // 否则播放列表中的下一首音乐
      currentSong = playlist(index + 1)
    }
  }

  def shufflePlayNext(): Unit = {
    // 从播放列表中随机选择一首音乐作为下一首
    currentSong = scala.util.Random.shuffle(playlist).headOption.getOrElse("")
  }

  def singlePlayNext(): Unit = {
    // 如果是单曲播放模式，则下一次播放还是当前音乐
    // 如果当前音乐不在播放列表中，则播放列表中的第一首音乐
    if (playlist.contains(currentSong)) {
      currentSong = currentSong
    } else {
      currentSong = playlist.headOption.getOrElse("")
    }
  }
}